﻿#include "SQLManagement.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QString>

SQLManagement* SQLManagement::m_pSqlmgt=NULL;
QSqlDatabase SQLManagement::m_database;

SQLManagement::SQLManagement()
{
    QString dataBasePath = "MyDataBase.db";

    if(QSqlDatabase::contains("qt_sql_default_connection"))
    {
        m_database = QSqlDatabase::database("qt_sql_default_connection");
    }
    else
    {
        m_database = QSqlDatabase::addDatabase("QSQLITE");
    }
    m_database.setDatabaseName(dataBasePath);
    if(!m_database.open())
    {
        qDebug()<<"database open failed."<<endl;
    }
    else
    {
        qDebug()<<"database open success."<<endl;
    }
}

//创建表格
int SQLManagement::CreatTable()
{
    QSqlQuery str_query(m_database);
    if(str_query.exec("sqlCmd"))
    {
        qDebug()<<"Error: create table failed."<<endl;
        return 0;
    }
    else
    {
        qDebug()<<"Success: create table success."<<endl;
        return 1;
    }
}

//插入数据
int SQLManagement::InsertICDData(ICD_INFO icdInfo)
{
    QSqlQuery str_query(m_database);
    QString strCmd = QString("INSERT INTO TABLE_NAME VALUES (%1,%2,%3,%4,%5,%6)").arg(icdInfo.icd_id).arg(icdInfo.icd_version)
            .arg(icdInfo.icd_revise_time).arg(icdInfo.icd_import_time).arg(icdInfo.icd_plane_type_id).arg(icdInfo.icd_file_path);
    if(!str_query.exec(strCmd))
    {
        qDebug()<<"Error: insert failed."<<endl;
        return 0;
    }
    else
    {
        qDebug()<<"Success: insert success."<<endl;
        return 1;
    }
}

//查询数据
QVector<ICD_INFO> SQLManagement::QueryAllICDInfoData()
{
    QVector<ICD_INFO> vecICDInfo;
    QSqlQuery str_query(m_database);
    if(!str_query.exec("SELECT * FROM ICDInfo"))
    {
        qDebug()<<"Error: query failed."<<endl;
    }
    else
    {
        while(str_query.next())
        {
            ICD_INFO icdinfo;
            icdinfo.icd_id = str_query.value(0).toInt();
            icdinfo.icd_version=str_query.value(1).toDouble();
            icdinfo.icd_revise_time = str_query.value(2).toString();
            icdinfo.icd_import_time = str_query.value(3).toString();
            icdinfo.icd_plane_type_id=str_query.value(4).toInt();
            icdinfo.icd_file_path = str_query.value(5).toString();
            vecICDInfo.push_back(icdinfo);
        }
        return vecICDInfo;
    }
}

//查询某一条ICD信息
ICD_INFO SQLManagement::QueryOneICDInfoData(int icdID)
{
    ICD_INFO icdinfo;
    QSqlQuery str_query(m_database);

    if(!str_query.exec("SELECT * FROM ICDInfo WHERE icd_id = "+icdID))
    {
        qDebug()<<"Error: query failed."<<endl;
    }
    else
    {
        icdinfo.icd_id = str_query.value(0).toInt();
        icdinfo.icd_version=str_query.value(1).toDouble();
        icdinfo.icd_revise_time = str_query.value(2).toString();
        icdinfo.icd_import_time = str_query.value(3).toString();
        icdinfo.icd_plane_type_id=str_query.value(4).toInt();
        icdinfo.icd_file_path = str_query.value(5).toString();
    }
    return icdinfo;
}

//删除某一条icd信息
int SQLManagement::DeleteICDInfoData(int icdID)
{
    QSqlQuery str_query(m_database);
    if(!str_query.exec("DELETE FROM ICDInfo WHERE icd_id = "+icdID))
    {
        qDebug()<<"Error: delete failed."<<endl;
        return 0;
    }
    else
    {
        qDebug()<<"Success: delete Success."<<endl;
        return 1;
    }
}

//析构函数
SQLManagement::~SQLManagement()
{
    m_database.close();
}
